/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.panomedic.gui;

import com.panomedic.log4j.Logger;
import java.io.File;
import java.io.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Yare
 */
public class FileChooser extends JFileChooser {
    
    static Logger logger = new Logger();
    
    private static boolean multiSelection = true;

    public FileChooser() {
        super();
        setMultiSelectionEnabled(true);
        setAccessory(new ImagePreview(this));
                
//TODO testing dir - saving to properties
        setCurrentDirectory(new File("D:\\School\\Rocnikovy projekt\\testimages"));

//TODO other format support        
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG Images", "png");
        setFileFilter(filter);
           
    }

    public File[] getFiles() {
        
        int returnVal = showOpenDialog(this);
        File files[] = null;        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            
            if (getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY){
                File dir = getSelectedFile();
                FileFilter filter = new FileFilter() {

                    public boolean accept(File pathname) {
                        return pathname.getName().toLowerCase().endsWith(".png");
                    }
                };
                files = dir.listFiles(filter);
                
            } else {
                files = getSelectedFiles();
            }
            
            
        }
        return files;        
    }
    
    public File getFile() {
        
        File file = null;
        file = new File("result.png");
        setSelectedFile(file);
        int returnVal = showSaveDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = getSelectedFile();
        } else file = null;
        return file;
                
    }

    public static void setMultiSelection(boolean multiSelection) {
        FileChooser.multiSelection = multiSelection;
    }

    @Override
    public void updateUI() {
        putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
        super.updateUI();
    }
    
    
    
}
